package org.itboys.mobile.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 包名：com.hz.sunday.chint.controller.utils
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2015年9月17日 下午5:27:19 
 * 描述:密码加密MD5
 *
 */
public class Md5UtilJx {
	  // 全局数组。不要随意变更
    private final static String[] strDigits = { "0", "X", "2", "3", "7", "5",
            "H", "7", "8", "Y", "a", "Z", "J", "W", "e", "N" };
    public Md5UtilJx() {
    }
    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }
    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }
    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }
  public static void main(String[] args) {
	  Md5UtilJx getMD5 = new Md5UtilJx();
        System.out.println(getMD5.GetMD5Code("111111"));
    }
}
